package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.UserAddressCliemt;
import com.hmall.common.client.AllClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.interceptorXinYing.BaseContext;
import com.hmall.order.mapper.OrderDerailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParam;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    AllClient allClient;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDerailMapper orderDerailMapper;

    @Autowired
    OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    UserAddressCliemt addressCliemt;

    //下单
    @Transactional
    @Override
    public Long itemOrder(RequestParam requestParam) {
        //- 1）根据雪花算法生成订单id

        Order order = new Order();
        //- 2）商品微服务提供FeignClient，实现根据id查询商品的接口
        Item item = allClient.selectById(requestParam.getItemId());
        //- 3）根据itemId查询商品信息
        //- 4）基于商品价格、购买数量计算商品总价：totalFee
        long totalFee = item.getPrice() * requestParam.getNum();
        //- 5）封装Order对象，初识status为未支付
        order.setStatus(1);
        order.setTotalFee(totalFee);
        order.setPaymentType(requestParam.getPaymentType());
        order.setId(BaseContext.getCurrentId());
        order.setTotalFee(order.getTotalFee());
        order.setUserId(order.getId());
        order.setCloseTime(new Date());
        order.setPayTime(new Date());
        order.setConsignTime(new Date());
        order.setEndTime(new Date());
        order.setCloseTime(new Date());
        order.setCommentTime(new Date());
        //- 6）将Order写入数据库tb_order表中
        orderMapper.insert(order);
        //- 7）将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(requestParam.getItemId());
        orderDetail.setNum(requestParam.getNum());
        orderDetail.setName(order.getTotalFee().toString());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDerailMapper.insert(orderDetail);
        //- 8）将user-service的根据id查询地址接口封装为FeignClient
        //- 9）根据addressId查询user-service服务，获取地址信息
        Long addressId = requestParam.getAddressId();
        Address address = addressCliemt.findAddressById(addressId);
        //- 10）将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(address.getId());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setMobile(address.getMobile());
        BeanUtils.copyProperties(address,orderLogistics);
       orderLogisticsMapper.insert(orderLogistics);
        //- 11）在item-service提供减库存接口，并编写FeignClient
        //- 12）调用item-service的减库存接口
        allClient.stock(requestParam.getItemId(),requestParam.getNum());
        return order.getId();
    }
}
